<? 
	include('base.php');
	User::protect();
	include_class('releases');
	include_class('m2');
	include_class('release_tracks');
	include_class('band_members');
	include_class('band_gps');
	$r = Release::get($_GET['id']);
	$section = 'releases';
	if (!db::isError($r)) {
		switch($_GET['task']) {
			case 'update':
				$res = $r->update($_POST);
				if (!db::isError($res)) {
					header('Location: release_edit.php?id=' . $_GET['id']);
				}
				break;
			case 'deactivate':
				$res = $r->deactivate();
				if (!db::isError($res)) {
					header('Location: release_edit.php?id=' . $_GET['id']);
				}
				break;
			case 'activate':
				$res = $r->activate();
				if (!db::isError($res)) {
					header('Location: release_edit.php?id=' . $_GET['id']);
				}
				break;
			case 'delete':
				$res = $r->remove();
				if (!db::isError($res)) {
					header('Location: releases.php');
				}
				break;
		}
	}
	$editors = array('notes','press_notes');
	$calendar = true;
	$page_title = 'Edit Release';
	include('layout/header.php');
?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="releases.php">Releases &#62;</a>
	View/Edit Release
</div>


<? 	if (!db::isError($r)) {
	
	if (db::isError($res)) {
		$res->outputList();
	}
	
	if (User::isAdmin() && ($_GET['task'] == 'edit' || $_GET['task'] == 'update')) { 
	
	$title = $_POST ? $_POST['title'] : $r->getTitle();
	$sub_title = $_POST ? $_POST['sub_title'] : $r->getSubTitle();
	$date = $_POST ? $_POST['date'] : $r->getDate();
	$notes = $_POST ? $_POST['notes'] : $r->getNotes();
	$press_notes = $_POST ? $_POST['press_notes'] : $r->getPressNotes();
	
	?>
		
		<h1>edit release:</h1>
		<div class="inset">
		<form id="edit_entry" action="<?=$PHP_SELF?>?task=update&amp;id=<?=$_GET['id']?>" method="post">
		<table border="0" class="edit-form" cellspacing="0" cellpadding="0">
		<? if (!$r->isActive()) { ?>
		<tr>	
			<td colspan="3" class="fieldname">This release is inactive.</td>
		</tr>
		<? } ?>
		<tr>
			<td width="33%"><strong>Title</strong><br/><input type="text" name="title" class="text" value="<?=$title?>"></td>
			<td width="34%"><strong>Sub Title (if applies)</strong><br/><input type="text" name="sub_title" class="text" value="<?=$sub_title?>"></td>
			<td width="33%"><strong>Release Date (<a style="vertical-align: middle" href="javascript:void(0)" onclick="popUpCalendar(this, document.getElementById('date'), '<?=DATE_JS_CALENDAR?>')">Calendar</a>)</strong><br/><input type="text" name="date" id="date" class="text" value="<?=$date?>"></td>
		</tr>
		</table>
		<div class="edit-form">
			<strong>Notes</strong><br/>
			<textarea name="notes" id="notes" class="full"><?=$notes?></textarea>
		</div>
		<div class="edit-form">
			<strong>Press Notes</strong><br/>
			<textarea name="press_notes" id="press_notes" class="full"><?=$press_notes?></textarea>
		</div>
		<div class="edit-form">
			<input type="submit" value="Update Release" class="button" name="submit">
			<input type="submit" value="Exit Without Updating" class="button" name="preview" onclick="location.href='<?=$PHP_SELF?>?id=<?=$r->getID()?>'; return false">
		</div>
		</div>
	
	<? } else { ?>

		<h1>view release:</h1>
		<div class="inset">
		
		<? $mi = $r->getCover();
			
			if ($mi) { ?>
			
				<div class="thumb-detail">
					<?=$mi->printThumbnail()?><br>(<a href="<?=$mi->getFileURL()?>" target="_blank" class="small">View/Download File</a>)
				</div>
			<? } 
		?>
		
		<h2><?=$r->getTitle()?></h2>
		<? if ($r->getSubTitle()) { ?>
			<h3><?=$r->getSubTitle()?></h3>
		<? } ?>
		
		<span class="small">
			Status: <strong><? if ($r->isActive()) { ?>Active<? } else { ?>Inactive<? } ?></strong>
			<span class="padded">|</span>
			Release Date: <strong><?=$r->getDate()?></strong>
		</span>
		
		<br/>
                <br/>

		<div style="width: 350px">
		<strong>Tracks</strong>
		<? if (User::isAdmin()) { ?>| <a href="release_track_add.php?id=<?=$_GET['id']?>">Add Track</a><? } ?>
		<br>

		<?
			$tracks = $r->getReleaseTracks();
			if (count($tracks) > 0) { ?>
			
			<table border="0" cellspacing="1" cellpadding="0" class="list">
			<?
				foreach($tracks as $rt) {
					echo('<tr><td>');
					echo($rt->getNumber());
					echo('</td><td width="100%">');
					echo("<a href=\"release_track_edit.php?id=".$rt->getID()."\">".$rt->getTitle()."</a>");
					echo('</td><td align="right">');
					if ($rt->lengthSet()) {
						echo($rt->getLength());
					} else {
						echo('&nbsp;');
					}
					echo('</td></tr>');					
				} 
			?>
			</table>	
			
			<?
			} else {
				echo("This release has no tracks.");
			}
		?>
		</div>

		<br/>

		<div style="width: 350px">
		<strong>Band members:</strong>
		<? if (User::isAdmin()) { ?>| <a href="javascript:void(0)" onclick="popWindow('release_band_members.php?id=<?=$r->getID()?>','450','450'); return false">Select Band Members</a><? } ?>
		<br>

		<?
			$bmArray = $r->getReleaseBandMembers();
			if (count($bmArray) > 0) { ?>
			
			<table border="0" cellspacing="1" cellpadding="0" class="list">
			<?
				foreach($bmArray as $bm) {
					$uo = $bm->getUserObject();
					echo('<tr><td>');
					echo("<a href=\"band_member_edit.php?id=".$bm->getID()."\">".$uo->getFirstName()." ".$uo->getLastName()."</a>");
					echo("</td><td>" . $bm->getRole());
					echo('</td></tr>');					
				} 
			?>
			</table>	
			
			<?
			} else {
				echo("This release has no band members associated with it.");
			}
		?>
		</div>

		<br/>

		<div style="width: 350px">
		<strong>Guest Performers:</strong>
		<? if (User::isAdmin()) { ?>| <a href="javascript:void(0)" onclick="popWindow('release_band_gps.php?id=<?=$r->getID()?>','450','450'); return false">Select Guest Performers</a><? } ?>
		<br>

		<?
			$gpArray = $r->getReleaseGuestPerformers();
			if (count($gpArray) > 0) { ?>
			
			<table border="0" cellspacing="1" cellpadding="0" class="list">
			<?
				foreach($gpArray as $gp) {
					echo('<tr><td>');
					echo("<a href=\"band_gp_edit.php?id=".$gp->getID()."\">".$gp->getFirstName()." ".$gp->getLastName()."</a>");
					echo("</td><td>" . $gp->getFunction());
					echo('</td></tr>');					
				} 
			?>
			</table>	
			
			<?
			} else {
				echo("This release has no guest performers associated with it.");
			}
		?>
		</div>

		<br/>

		<div style="width: 350px">
		<strong>Notes</strong><br>
		<? if ($r->getNotes()) { ?>
			<?=$r->getNotes()?>
		<? } else { ?>
			None
		<? } ?>
                </div>

		<br/>

		<div style="width: 350px">
		<strong>Press Notes</strong><br>
		<? if ($r->getPressNotes()) { ?>
			<?=$r->getPressNotes()?>
		<? } else { ?>
			None
		<? } ?>
		</div>

                <br/>

		<form name="entry_status" method="get">
			<? if (User::isAdmin()) { ?>
				<input type="submit" name="" value="Edit" onclick="location.href='<?=$PHP_SELF?>?task=edit&id=<?=$_GET['id']?>';return false" class="button">
			<? } ?>
			<input type="button" name="" value="Photos" onclick="location.href='release_media.php?id=<?=$_GET['id']?>&type=photos';return false" class="button">
			<input type="button" name="" value="Audio/Video" onclick="location.href='release_media.php?id=<?=$_GET['id']?>&type=av';return false" class="button">

			<? if (User::isAdmin()) { ?>			
				<? if ($r->isActive()) { ?><input type="submit" name="" value="Deactivate" onclick="location.href='<?=$PHP_SELF?>?task=deactivate&id=<?=$_GET['id']?>';return false" class="button"><? } else { ?><input type="submit" name="" value="Activate" onclick="location.href='<?=$PHP_SELF?>?task=activate&id=<?=$_GET['id']?>';return false" class="button"><? } ?>
				<input type="submit" value="Remove" name="" onclick="if (confirm('Are you sure you wish to delete this release?\rWhenever possible, deactivate rather than delete.')) { location.href='<?=$PHP_SELF?>?task=delete&id=<?=$_GET['id']?>'};return false" class="button">
			<? } ?>
			<input type="submit" value="Return to Releases" name="" onclick="location.href='releases.php'; return false" class="button">
		</form>

                </div>
	<? } ?>

<? } else { 
	$r->outputDialog('Return to Releases','releases.php');
} ?>

<? include('layout/footer.php'); ?>
